package ws.jdbc.concurrent.driver.sqlserver;

import java.sql.ResultSet;
import java.sql.SQLException;
import ws.jdbc.concurrent.driver.sqlserver.tds.TDSMessage;

/**
 * This statement creates FORWARD-ONLY and READ-ONLY result sets.
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class CStatementForwardOnlyReadOnly extends CStatement {

  /**
   * Create new statement instance.
   *
   * @param id id of the statement.
   * @param connection Connection object that created this statement.
   * @param message TDS message object with the connection established with the server.
   */
  protected CStatementForwardOnlyReadOnly(final int id, final CConnection connection, final TDSMessage message) {
    super(id, connection, message, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
  }

  @Override
  public synchronized ResultSet executeQuery(String sql) throws SQLException {
    throwExceptionIfClosed();
    closeResultSet();
    resultSet = new CResultSetForwardOnlyReadOnly(message, this, sql);
    return resultSet;
  }

  public synchronized CResultSet getCursor() throws SQLException {
    throwExceptionIfClosed();
    throwExceptionIfResultSetIsClosed();
    if (shared) {
      return new ws.jdbc.concurrent.driver.sqlserver.cursor.shared.CursorForwardOnlyReadOnly(message, resultSet, lock);
    } else {
      if (individualCursor == null) {
        individualCursor = new ws.jdbc.concurrent.driver.sqlserver.cursor.individual.CursorForwardOnlyReadOnly(message, resultSet, lock);
      }
      return individualCursor;
    }
  }
}
